package com.derek.omshopping.ui.home;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.derek.omshopping.R;
import com.derek.omshopping.base.BaseFragment;
import com.derek.omshopping.ui.home.adapter.FragmentMainTabAdapter;
import com.derek.omshopping.ui.home.view.BestMondayFragment;
import com.derek.omshopping.ui.home.view.GlobalFragment;
import com.derek.omshopping.ui.home.view.MemberFragment;
import com.derek.omshopping.ui.home.view.ShouYeFragment;
import com.derek.omshopping.ui.home.view.TeHuiFragment;
import com.derek.omshopping.ui.home.view.TuanGouFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by asus on 2016/10/17 09 49.
 * 邮箱:763433963@qq.com
 * 该fragment用于实现主页页面各功能
 */

public class HomeFragment extends BaseFragment {

    @BindView(R.id.tab_main_tabLayout)
    TabLayout tabMainTabLayout;
    @BindView(R.id.viewPager_main_home)
    ViewPager viewPagerMainHome;

    private List<Fragment> fragmentList = new ArrayList<>();
    private String[] arrTabTitles = null;
    private FragmentMainTabAdapter fragmentAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
            savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    protected void initData() {
        arrTabTitles = getResources().getStringArray(R.array.home_arrTabTitles);
        initTab();
        initVP();

    }

    //初始化tabLayout
    private void initTab() {
        //设置tablayout的选中状态
        for (int i = 0; i < arrTabTitles.length; i++) {
            if (i == 0) {
                tabMainTabLayout.addTab(tabMainTabLayout.newTab().setText(arrTabTitles[i]), true);
            } else {
                tabMainTabLayout.addTab(tabMainTabLayout.newTab().setText(arrTabTitles[i]), false);
            }
            tabMainTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        }

        // 添加viewpager的监听 参数传递tabLayout中提供的监听事件  viewpager中的page页与tab对应
        viewPagerMainHome.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabMainTabLayout));
        //设置tabLayout的监听事件  滑动改变tabLayout中的tab时viewpager跟随变化
        tabMainTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPagerMainHome.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }

        });
    }

    @Override
    protected void initView() {

    }

    //初始化适配viewpager的数据
    public void initVP() {
        ShouYeFragment f1 = new ShouYeFragment();
       /* Bundle bundle = new Bundle();
        bundle.putInt("kind", 1);
        f1.setArguments(bundle);*/
        TuanGouFragment f2 = new TuanGouFragment();
        GlobalFragment f3 = new GlobalFragment();
        TeHuiFragment f4 = new TeHuiFragment();
        BestMondayFragment f5 = new BestMondayFragment();
        MemberFragment f6 = new MemberFragment();


        fragmentList.add(f1);
        fragmentList.add(f2);
        fragmentList.add(f3);
        fragmentList.add(f4);
        fragmentList.add(f5);
        fragmentList.add(f6);
        fragmentAdapter = new FragmentMainTabAdapter(getFragmentManager(), fragmentList);
        viewPagerMainHome.setAdapter(fragmentAdapter);
    }

}
